package com.atguigu.Spring5.dao;

import com.atguigu.Spring5.pojo.Ld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiao hu die
 * @Date 2022/4/1 15:04
 * @Version 1.0
 */
@Repository
public class ChengWeiYiDaoImpl implements ChengWeiYiDao{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public  Integer Insert(Ld ld){
     String sql ="INSERT INTO ld VALUES(?,?,?)";
        int update = jdbcTemplate.update(sql, ld.getId(), ld.getName(), ld.getAge());
        return update;
    }

    @Override
    public Integer Delete(Integer integer) {
        String sql ="DELETE FROM ld where id =?";
        int update = jdbcTemplate.update(sql, integer);
        return update;
    }

    @Override
    public Integer Update(Ld ld) {
        String sql ="UPDATE ld SET name =?,age =? WHERE id =?";
        Object[] args ={ld.getName(),ld.getAge(),ld.getId()};
        int update = jdbcTemplate.update(sql, args);
        return update;
    }

    @Override
    public Ld findOne(Integer integer) {
        String sql ="SELECT * from ld where id =?";
        Ld ld = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Ld>(Ld.class), integer);
        return  ld;

    }

    @Override
    public List<Ld> findAll() {
        List ldList =new ArrayList();
        String sql ="SELECT * FROM ld";
        List<Ld> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Ld>(Ld.class));
        return query;
    }

    @Override
    public Integer jianshao() {
        String sql ="UPDATE  t_account SET money = money -100 WHERE name =?";
        int i = jdbcTemplate.update(sql, "陈唯怡");
        return i;
    }

    @Override
    public Integer tianjia() {
        String sql ="UPDATE  t_account SET money = money +100 WHERE name =?";
        int i = jdbcTemplate.update(sql, "刘碟");
        return i;
    }

}
